/**********************************************************************************
// Sprite Animation Source
// 
// Creation:	20 Apr 2011
// Updated:		02 Jun 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Defines a sprite animation engine
//
**********************************************************************************/

#include "Animation.h"

/**********************************************************************************/
// constructor of animation
// TODO: allocate space for the sequence and copy it from the argument
Animation::Animation(const uint * seq, uint seqSize, uint delay, bool follow, float x, float y, uint repeat)
{
	// initial (x,y) coordinates of animation
	xPos = x;
	yPos = y;

	// start always in the first frame
	currentFrame = startFrame = 0;		

	// set final frame to the last frame
	endFrame = seqSize - 1;
	
	// time delay between frames
	animDelay = delay;

	// animation mode defaults to ONETIMER
	animMode = repeat;

	// animation should follow sprite position or not
	followMode = follow;

	// set animation sequence
	if (seq)
	{
		sequence = new uint[seqSize];
		for (uint i = 0; i < seqSize; ++i)
			sequence[i] = seq[i];
	}
	else
		sequence = nullptr;

	// initialize with object creation time
	timestamp = GetTickCount();				
}

/**********************************************************************************/

Animation::~Animation()
{
	if (sequence)
		delete [] sequence;
}

/**********************************************************************************/

void Animation::NextFrame()
{
	if (int(GetTickCount()) > timestamp + animDelay)
	{
		currentFrame++;
		if (animMode == ENDLESS && currentFrame > endFrame)
			currentFrame = 0;
		timestamp = GetTickCount();		
	}
}

/**********************************************************************************/